'use strict';

const Controller = require('egg').Controller;

const util = require('utility');
class commonController extends Controller {

  async bdSearch() {
    const { ctx } = this;
    const { address,cite } = ctx.request.body;
    const bmapak = 'GjHSdjNPnLkQGbhFnjmD9FKNTVor3yXs'; // 服务端的key
    // 使用到的接口文档地址 https://lbsyun.baidu.com/index.php?title=webapi/place-suggestion-api
    console.log(address,cite,'我是地址和关键字');
    const res = await ctx.curl(`https://api.map.baidu.com/place/v2/suggestion?query=${address}&region=${cite}&city_limit=true&output=json&ak=${bmapak}`, {
      dataType: 'json',
    });
    console.log(res,'我是结果');

    ctx.body = { success: true, data: res.data };
  }

 




  async upload() {
    const { ctx } = this;
    const file = ctx.request.files[0]; // 得到上传的图片
    const ext = file.filename.split('.').pop(); // 得到文件后缀

    const _date = util.YYYYMMDD(''); // 20211219
    const type = 'common';
    const normalPath = `${type}/${_date}`; // 路径 common/20211229
    const newFileName = Date.now() + '.' + ext;
    try {
      // 处理文件，比如上传到云端
      console.log('do upload to the oss');
      //  ctx.oss.put(新的文件路径,要上传的文件);
      const res = await ctx.oss.put(normalPath + '/' + newFileName, file.filepath);

      console.log(res);
      console.log('end');
      ctx.body = { success: true, data: 'http://fang.1775.net.cn/' + normalPath + '/' + newFileName };
    } catch (e) {
      console.log(e);
      ctx.body = { success: false, msg: e };
    } finally {
      // await fs.unlinkSync(file.filepath);
    }

  }
}

module.exports = commonController;
